package com.zyk.leetcode;

import java.util.Arrays;

/**
 * @author zhangsan
 * @date 2021/4/5 19:42
 */
public class C88 {

    // https://leetcode-cn.com/problems/merge-sorted-array/
    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int[] help = new int[nums1.length];
        int l1=0, l2 = 0, helpIndex = 0;
        while (l1 < m && l2 < n) {
            help[helpIndex++] = nums1[l1] <= nums2[l2]? nums1[l1++]: nums2[l2++];
        }
        // 出来两种情况 l1 越界, l2越界
        while (l1 < m) {
            help[helpIndex++] = nums1[l1++];
        }
        while (l2 < n) {
            help[helpIndex++] = nums2[l2++];
        }
        while (--helpIndex >= 0) {
            nums1[helpIndex] = help[helpIndex];
        }
    }


    // for test
    public static void main(String[] args) {
        int[] a = {1,2,3,0,0,0};
        int[] b = {2,5,6};
        merge(a, 3, b, 3);
        System.out.println(Arrays.toString(a));
    }

}
